package com.xioz.android;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector3;

/**
 * MenuScreen Class
 * Screen class that holds Menu options
 * 
 * @author Zong Xiong
 */
public class MenuScreen implements Screen{
	
/***************************************************
 * Fields
 ***************************************************/
	
	// camera for viewing
	private OrthographicCamera camera;
	// batch for drawing textures
	private SpriteBatch batch;
	// parent game
	private Game game;
	// fonts
	private BitmapFont menuFont, textFont;
	// pointer to store where screen touched
	private Vector3 pointer;
	// texture stores menu textures
	private Texture menuAtlas;
	// texture regions from the atlas
	private TextureRegion background, inactiveButton, activeButton, textBar, grayBar, backArrowTexture;
	// button for backArrow
	private GameObject backArrow;
	// stores the currentMenu option
	private MenuType currentMenu;
	
/***************************************************
 * Public Methods
 ***************************************************/
	
	/**
	 * Default Constructor
	 * calls superclass constructor then own initialization
	 */
	public MenuScreen(Game inGame) {
		// loads parent game
		game = inGame;
		
		// creates batch for drawing
		batch = new SpriteBatch();
		// creates pointer for storage
		pointer = new Vector3();
		
		// loads textures and fonts
		menuAtlas = new Texture("data/backgrounds/menuatlas.png");
		menuFont = new BitmapFont(Gdx.files.internal("data/fonts/sans40.fnt"), false);
		textFont = new BitmapFont(Gdx.files.internal("data/fonts/sans20.fnt"), false);
		background = new TextureRegion(menuAtlas, 0, 32, 320, 480);
		backArrowTexture = new TextureRegion(menuAtlas, 480, 480, 32, 32);
		inactiveButton = new TextureRegion(menuAtlas, 320, 272, 80, 240);
		activeButton = new TextureRegion(menuAtlas, 400, 272, 80, 240);
		textBar = new TextureRegion(menuAtlas, 320, 228, 104, 44);
		grayBar = new TextureRegion(menuAtlas, 424, 228, 88, 44);
		backArrow = new GameObject(288, 0, 32, 32);
		
		// loads MainMenu
		currentMenu = new MainMenu(null);
	}

	/** {@inheritDoc} */
	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	/** {@inheritDoc} */
	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	/** {@inheritDoc} */
	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	/** {@inheritDoc} */
	@Override
	public void render(float delta) {
		//separate rendering to draw and update
		draw();
		update();
	}

	/** {@inheritDoc} */
	@Override
	public void resize(int width, int height) {
        camera = new OrthographicCamera(width, height);
        camera.position.set(width / 2,height / 2,0);
	}

	/** {@inheritDoc} */
	@Override
	public void show() {
		resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		render(Gdx.graphics.getDeltaTime());
	}

	/** {@inheritDoc} */
	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}
	
/***************************************************
 * Private Methods
 ***************************************************/
	
	/**
	 * update method
	 * checks for any updates needed for the screen
	 */
	private void update() {
		// check for input via touch screen
		if (Gdx.input.justTouched()) {
			// loads position to pointer
			camera.unproject(pointer.set(Gdx.input.getX(), Gdx.input.getY(), 0));
			
			// create a temporary menu for the potential sub-menu
			MenuType newMenu = currentMenu.getMenuOption(pointer);
			
			// else the backArrow was pressed, we go to the previous menu
			if (backArrow.contains(pointer) ) {
				newMenu = currentMenu.getPreviousMenu();
			}
			// if the newMenu is not null, we load it as the new current menu
			if (newMenu != null) {
				currentMenu = newMenu;
			}
			// in the case of loading a wordGame, we set the screen of the parent game
			if (newMenu instanceof GameAdvanceMenu) {
				Screen newScreen = ((GameAdvanceMenu) newMenu).getWordGame();
				((WordGameScreen) newScreen).setGame(game);
				game.setScreen(newScreen);
			}
		}
	}
	
	private void draw() {
		// update and set camera to batch
		camera.update();
		batch.setProjectionMatrix(camera.combined);
		
		// begin drawing textures
		batch.begin();
		
		// draw background and backArrow
		batch.draw(background, 0 , 0);
		batch.draw(backArrowTexture, backArrow.getX(), backArrow.getY());
		
		// draw options for the current Menu
		currentMenu.draw(batch, inactiveButton, activeButton, textBar, grayBar, menuFont, textFont);
		
		// end drawing textures
		batch.end();
	}
}
